package cn.cmdr.controller;


import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.Role;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.PermissionService;
import cn.cmdr.service.RoleService;
import cn.cmdr.service.StaffService;
import cn.cmdr.vo.ResultVo;
import cn.cmdr.vo.RoleVo;
import cn.cmdr.vo.TreeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/jur/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private StaffService staffService;
    @Resource
    private JournalService journalService;
    @PostMapping("/rolelist")
    public ResultVo getRoleList(){
        List<Role> list = roleService.getRoleList();
        return ResultVo.sucess(list);
    }
    @PostMapping("/add")
    @RequiresPermissions("role:add")
    public ResultVo add(@RequestBody Role role){
        try{
            if(role.getRoleName()==null){
                return ResultVo.fail("角色名不能为空！！");
            }
            if(role.getDescrib()==null){
                return ResultVo.fail("角色描述不能为空！！");
            }
            role.setCreationDate(new Date());
            roleService.add(role);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加角色:"+role.getRoleName());
            journal.setMenuId(8);
            journalService.add(journal);
            return ResultVo.sucess("角色添加成功!");
        }catch (Exception e){
            return ResultVo.fail("角色添加失败了",e);//包含了打印错误日志
        }
    }
    //删除
    @GetMapping("/del/{id}")
    @RequiresPermissions("role:del")
    public ResultVo del(@PathVariable String id){
        try{
            List<Staff> list = staffService.getById(Integer.parseInt(id));
            Role role = roleService.getrole(Integer.parseInt(id));
            if(list.size()!=0){
                return ResultVo.fail(role.getRoleName()+"下还有员工不能删除!");
            }else {
                roleService.deleteById(Integer.parseInt(id));
                Journal journal = new Journal();
                journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
                journal.setUpDate(new Date());
                journal.setUpContent("删除角色:"+role.getRoleName());
                journal.setMenuId(8);
                journalService.add(journal);
            }
            return ResultVo.sucess("角色删除成功！");
        }catch (Exception e){
            return ResultVo.fail("角色删除失败！",e);
        }
    }
    //修改
    @PostMapping("/update")
    @RequiresPermissions("role:update")
    public ResultVo update(@RequestBody Role role){
        try{
            roleService.update(role);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加角色:"+role.getRoleName());
            journal.setMenuId(8);
            journalService.add(journal);
            return ResultVo.sucess("角色修改成功");
        }catch (Exception e){
            return ResultVo.fail("角色修改失败",e);
        }
    }
    //分页
    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getList(@PathVariable Integer pageSize,@PathVariable Integer pageNo,@RequestBody RoleVo roleVo){
        return ResultVo.sucess(roleService.getPageRoleList(pageNo,pageSize,roleVo));
    }
    @GetMapping("/grant")
    @RequiresPermissions("role:grant")
    public ResultVo grant(Integer roleId){
        List<TreeNode> list = permissionService.getTreeNode(roleId);
        return ResultVo.sucess(list);
    }
    @GetMapping("/dogrant")
    @RequiresPermissions("role:grant")
    public ResultVo dogrant(Integer roleId,Integer[] nid){
        try{
            permissionService.dogrant(roleId,nid);
            return ResultVo.sucess("角色授权成功");
        }catch (Exception e){
            return ResultVo.fail("角色授权失败",e);
        }
    }
}
